package com.ysy.tz.mfg.controller;

import com.github.pagehelper.PageInfo;
import com.ysy.tz.core.annotation.Service;
import com.ysy.tz.core.annotation.ShowLoading;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.engine.DefaultKeyValuePair;
import com.ysy.tz.core.engine.KeyValuePair;
import com.ysy.tz.core.excel.ExcelData;
import com.ysy.tz.core.excel.ExcelOutHandler;
import com.ysy.tz.core.selfcomp.CommonDialog;
import com.ysy.tz.core.selfcomp.CustomerAlert;
import com.ysy.tz.core.selfcomp.CustomerComboBox;
import com.ysy.tz.core.selfcomp.CustomerPagination;
import com.ysy.tz.core.selfcomp.PopupTextField;
import com.ysy.tz.core.selfcomp.TextChangeListener;
import com.ysy.tz.core.utils.FormUtils;
import com.ysy.tz.core.utils.ModelUtils;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.dao.DataListDao;
import com.ysy.tz.mfg.dto.InspectionWeightDto;
import com.ysy.tz.mfg.entity.DataListEntity;
import com.ysy.tz.mfg.entity.FlightsEntity;
import com.ysy.tz.mfg.entity.InspectionWeightEntity;
import com.ysy.tz.mfg.entity.UserEntity;
import com.ysy.tz.mfg.service.InspectionWeightService;
import com.ysy.tz.mfg.utils.DefaultPropertyValueFactory;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.AccessibleAttribute;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputControl;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.effect.BlendMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.text.Font;
import org.apache.commons.collections4.CollectionUtils;

public class InspectionWeightController implements Initializable {
    @FXML
    private BorderPane queryForm;
    @FXML
    private TextField pigshipno;
    @FXML
    ComboBox slaughterType;
    @FXML
    ComboBox inPigSty;
    @FXML
    ComboBox isRiskAssessment;
    @FXML
    ComboBox isDisabledPig;
    @FXML
    ComboBox flightsCode;
    @FXML
    ComboBox selfSupplyCode;
    @FXML
    ComboBox pigTypeCode;
    @FXML
    TableView<InspectionWeightEntity> inspectionWeightTableView;
    @FXML
    TableView<InspectionWeightEntity> sumInspectionWeightTableView;
    @FXML
    CustomerPagination inspectionWeightTablePagination;
    @Service
    InspectionWeightService inspectionWeightService;
    @FXML
    TableColumn tb_slaughterType;
    @FXML
    TableColumn tb_pigTypeCode;
    @FXML
    TableColumn tb_disabledPig;
    @FXML
    TableColumn tb_riskAssessment;
    @FXML
    TableColumn tb_hasSalesCommitment;
    ResourceBundle resourceBundle;
    ExcelOutHandler excelOutHandler = null;
    Dialog takeToExperimentDialog = null;
    Dialog pigTypeChangeDialog = null;

    public InspectionWeightController() {
    }

    public void initialize(URL location, ResourceBundle resources) {
        this.resourceBundle = resources;
        ObservableList<DefaultKeyValuePair> defaultKeyValuePairArrayList = FXCollections.observableArrayList(new DefaultKeyValuePair[]{new DefaultKeyValuePair("Y", ApplicationContext.commonBundle.getString("yes")), new DefaultKeyValuePair("N", ApplicationContext.commonBundle.getString("no"))});
        this.inPigSty.setItems(defaultKeyValuePairArrayList);
        this.isDisabledPig.setItems(defaultKeyValuePairArrayList);
        this.isRiskAssessment.setItems(defaultKeyValuePairArrayList);
        ArrayList<FlightsEntity> flightsList = (ArrayList)SessionUtils.getAttribute(SessionUtils.FLIGHTS);
        this.flightsCode.setItems(FXCollections.observableArrayList(flightsList));
        SQLUtils.executeSQL(DataListDao.class, (dataListDao) -> {
            DataListEntity dataListEntity = new DataListEntity();
            dataListEntity.setListTypeCode("SelfSupply");
            this.selfSupplyCode.setItems(FXCollections.observableArrayList(dataListDao.searchDataListByTypeCode(dataListEntity)));
        });
        ObservableList<DefaultKeyValuePair> slaughterTypeArrayList = FXCollections.observableArrayList(new DefaultKeyValuePair[]{new DefaultKeyValuePair("M", ApplicationContext.commonBundle.getString("pigTypeM")), new DefaultKeyValuePair("T", ApplicationContext.commonBundle.getString("pigTypeT")), new DefaultKeyValuePair("J", ApplicationContext.commonBundle.getString("pigTypeJ"))});
        this.slaughterType.setItems(slaughterTypeArrayList);
        this.tb_slaughterType.setCellValueFactory(new DefaultPropertyValueFactory("tb_slaughterType", slaughterTypeArrayList));
        ObservableList<DefaultKeyValuePair> pigTypeArrayList = FXCollections.observableArrayList(this.inspectionWeightService.searchPigType());
        this.pigTypeCode.setItems(pigTypeArrayList);
        this.tb_pigTypeCode.setCellValueFactory(new DefaultPropertyValueFactory("tb_pigTypeCode", pigTypeArrayList));
        this.tb_disabledPig.setCellValueFactory(new DefaultPropertyValueFactory("tb_disabledPig", defaultKeyValuePairArrayList));
        this.tb_riskAssessment.setCellValueFactory(new DefaultPropertyValueFactory("tb_riskAssessment", defaultKeyValuePairArrayList));
        this.tb_hasSalesCommitment.setCellValueFactory(new DefaultPropertyValueFactory("tb_hasSalesCommitment", defaultKeyValuePairArrayList));
        this.excelOutHandler = new ExcelOutHandler() {
            public ExcelData exportSegment(int pageNum, int pageSize) {
                try {
                    InspectionWeightDto condition = (InspectionWeightDto)FormUtils.collectParams(InspectionWeightController.this.queryForm, InspectionWeightDto.class);
                    condition.setPageNum(pageNum);
                    condition.setPageSize(pageSize);
                    PageInfo<InspectionWeightEntity> datas = InspectionWeightController.this.inspectionWeightService.searchInspectionWeightList(condition);
                    if (pageNum == 1 && datas != null && CollectionUtils.isNotEmpty(datas.getList())) {
                        ObservableList tableData = FXCollections.observableArrayList(InspectionWeightController.this.inspectionWeightService.searchInspectionWeightSum(condition));
                        CollectionUtils.addAll(datas.getList(), tableData);
                    }

                    return new ExcelData((List)InspectionWeightController.this.inspectionWeightTableView.getColumns().stream().filter((tableColumn) -> {
                        return !tableColumn.getId().equals("tableSeqNo");
                    }).collect(Collectors.toList()), datas);
                } catch (Exception var6) {
                    var6.printStackTrace();
                    return null;
                }
            }
        };
    }

    public void reset() {
        FormUtils.resetFrom(this.queryForm);
    }

    @ShowLoading
    public void query() {
        try {
            InspectionWeightDto condition = (InspectionWeightDto)FormUtils.collectParams(this.queryForm, InspectionWeightDto.class);
            ObservableList tableData = FXCollections.observableArrayList(this.inspectionWeightService.searchInspectionWeightSum(condition));
            this.sumInspectionWeightTableView.setItems(tableData);
            condition.setPageNum(this.inspectionWeightTablePagination.getCurrentPageIndex() + 1);
            condition.setPageSize(this.inspectionWeightTablePagination.getPageSize());
            PageInfo<InspectionWeightEntity> inspectionWeightList = this.inspectionWeightService.searchInspectionWeightList(condition);
            ModelUtils.setTableViewValue(inspectionWeightList, this.inspectionWeightTableView, this.inspectionWeightTablePagination);
            ScrollBar scrollBar1 = (ScrollBar)this.sumInspectionWeightTableView.queryAccessibleAttribute(AccessibleAttribute.HORIZONTAL_SCROLLBAR, new Object[0]);
            ScrollBar scrollBar2 = (ScrollBar)this.inspectionWeightTableView.queryAccessibleAttribute(AccessibleAttribute.HORIZONTAL_SCROLLBAR, new Object[0]);
            if (null != scrollBar1 && null != scrollBar2) {
                scrollBar1.valueProperty().bindBidirectional(scrollBar2.valueProperty());
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }

    public void showTypeChangeDialog() {
        InspectionWeightEntity inspectionWeightEntity = (InspectionWeightEntity)this.inspectionWeightTableView.getSelectionModel().getSelectedItem();
        if (inspectionWeightEntity == null) {
            (new CustomerAlert(AlertType.CONFIRMATION, ApplicationContext.commonBundle.getString("choise_one"), new ButtonType[0])).showAndWait();
        } else {
            this.pigTypeChange(inspectionWeightEntity).show();
        }

    }

    public void showTakeToExperimentDialog() {
        InspectionWeightEntity inspectionWeightEntity = (InspectionWeightEntity)this.inspectionWeightTableView.getSelectionModel().getSelectedItem();
        if (inspectionWeightEntity == null) {
            (new CustomerAlert(AlertType.CONFIRMATION, ApplicationContext.commonBundle.getString("choise_one"), new ButtonType[0])).showAndWait();
        } else {
            this.takeToExperiment(inspectionWeightEntity).show();
        }

    }

    private Dialog takeToExperiment(InspectionWeightEntity inspectionWeightEntity) {
        if (this.takeToExperimentDialog == null) {
            this.takeToExperimentDialog = new Dialog();
            DialogPane dialogPane = new DialogPane();
            dialogPane.setPrefSize(360.0, 300.0);
            dialogPane.getButtonTypes().addAll(new ButtonType[]{ButtonType.OK, ButtonType.CANCEL});
            Font font = new Font(16.0);
            GridPane gridPane = new GridPane();
            gridPane.setPadding(new Insets(10.0, 10.0, 0.0, 10.0));
            gridPane.setVgap(10.0);
            gridPane.setHgap(5.0);
            Label slaughterNumberLabel = new Label(this.resourceBundle.getString("tb_slaughterNumber"));
            slaughterNumberLabel.setMinWidth(100.0);
            slaughterNumberLabel.setFont(font);
            Label slaughterNumberValueLabel = new Label(inspectionWeightEntity.getSlaughterNo());
            slaughterNumberValueLabel.setStyle("-fx-text-fill: #f05b72; -fx-font-weight: bold; -fx-font-size: 16;");
            slaughterNumberValueLabel.setFont(font);
            gridPane.add(slaughterNumberLabel, 0, 0);
            GridPane.setHgrow(slaughterNumberLabel, Priority.SOMETIMES);
            gridPane.add(slaughterNumberValueLabel, 1, 0);
            GridPane.setHgrow(slaughterNumberValueLabel, Priority.SOMETIMES);
            Label trialNumberLabel = new Label(this.resourceBundle.getString("tb_trialNumber"));
            trialNumberLabel.setFont(font);
            ComboBox<String> experimentNumberCombo = new ComboBox();
            String[] seqnumbers = new String[6];
            String workday = ((String)SessionUtils.getAttribute(SessionUtils.WORK_DAY)).replaceAll("-", "").substring(2, 8);

            for(int i = 0; i < 6; ++i) {
                seqnumbers[i] = ApplicationContext.enterpriseCode + workday + "0" + i;
            }

            experimentNumberCombo.setItems(FXCollections.observableArrayList(seqnumbers));
            experimentNumberCombo.setStyle("-fx-font-size: 18;");
            gridPane.add(trialNumberLabel, 0, 1);
            gridPane.add(experimentNumberCombo, 1, 1);
            Label experimentTypeLabel = new Label(this.resourceBundle.getString("experimentType"));
            experimentTypeLabel.setFont(font);
            CustomerComboBox experimentTypeComboBox = new CustomerComboBox();
            experimentTypeComboBox.setStyle("-fx-font-size: 18;");
            gridPane.add(experimentTypeLabel, 0, 2);
            gridPane.add(experimentTypeComboBox, 1, 2);
            ObservableList<KeyValuePair> pigTypeArrayList = FXCollections.observableArrayList(this.inspectionWeightService.searchExperimentTypeList());
            experimentTypeComboBox.setItems(pigTypeArrayList);
            Label experimentItemCodeLabel = new Label(this.resourceBundle.getString("experimentCode"));
            experimentItemCodeLabel.setFont(font);
            final PopupTextField experimentItemCodeValueLabel = new PopupTextField();
            experimentItemCodeValueLabel.setFont(font);
            TableColumn tableColumn = new TableColumn(this.resourceBundle.getString("experimentCode"));
            tableColumn.setId("qr_keyPair");
            tableColumn.setPrefWidth(150.0);
            experimentItemCodeValueLabel.setTableColumns(tableColumn);
            tableColumn = new TableColumn(this.resourceBundle.getString("experimentName"));
            tableColumn.setId("qr_valuePair");
            tableColumn.setPrefWidth(150.0);
            experimentItemCodeValueLabel.setTableColumns(tableColumn);
            gridPane.add(experimentItemCodeLabel, 0, 3);
            GridPane.setHgrow(experimentItemCodeLabel, Priority.SOMETIMES);
            gridPane.add(experimentItemCodeValueLabel, 1, 3);
            GridPane.setHgrow(experimentItemCodeValueLabel, Priority.SOMETIMES);
            Label experimentItemNameLabel = new Label(this.resourceBundle.getString("experimentName"));
            experimentItemNameLabel.setFont(font);
            final TextField experimentItemNameValueLabel = new TextField();
            experimentItemNameValueLabel.setFont(font);
            experimentItemNameValueLabel.setEditable(false);
            experimentItemNameValueLabel.setBlendMode(BlendMode.DARKEN);
            gridPane.add(experimentItemNameLabel, 0, 4);
            GridPane.setHgrow(experimentItemNameLabel, Priority.SOMETIMES);
            gridPane.add(experimentItemNameValueLabel, 1, 4);
            GridPane.setHgrow(experimentItemNameValueLabel, Priority.SOMETIMES);
            experimentItemCodeValueLabel.addTextChangeListener(new TextChangeListener<DefaultKeyValuePair>() {
                public ObservableList textChanged(TextInputControl textField, String newValue) {
                    return (ObservableList)SQLUtils.executeSQLWithReturn(DataListDao.class, (dataListDao) -> {
                        try {
                            ArrayList<DefaultKeyValuePair> defaultKeyValuePairs = dataListDao.searchExperimentList(ApplicationContext.enterpriseId, newValue);
                            return FXCollections.observableList(defaultKeyValuePairs);
                        } catch (Exception var3) {
                            var3.printStackTrace();
                            return null;
                        }
                    });
                }

                public void PopupChoise(DefaultKeyValuePair defaultKeyValuePair) {
                    experimentItemCodeValueLabel.setText(defaultKeyValuePair.getKeyPair());
                    experimentItemNameValueLabel.setText(defaultKeyValuePair.getValuePair());
                }
            });
            Label remarkLabel = new Label(this.resourceBundle.getString("tb_remark"));
            remarkLabel.setFont(font);
            TextField remarkTextField = new TextField();
            remarkTextField.setFont(font);
            gridPane.add(remarkLabel, 0, 5);
            GridPane.setHgrow(remarkLabel, Priority.SOMETIMES);
            gridPane.add(remarkTextField, 1, 5);
            GridPane.setHgrow(remarkTextField, Priority.SOMETIMES);
            dialogPane.setContent(gridPane);
            this.takeToExperimentDialog.setDialogPane(dialogPane);
            this.takeToExperimentDialog.setTitle(this.resourceBundle.getString("btn_type_change"));
            this.takeToExperimentDialog.initOwner(ApplicationContext.mainStage);
            Button okBtn = (Button)dialogPane.lookupButton(ButtonType.OK);
            okBtn.setOnMousePressed((event) -> {
                InspectionWeightEntity target = new InspectionWeightEntity();
                target.setEnterpriseId(ApplicationContext.enterpriseId);
                target.setExperimentOrderNumber((String)experimentNumberCombo.getSelectionModel().getSelectedItem());
                target.setSlaughterNo(slaughterNumberValueLabel.getText());
                target.setExperimentTypeCode(((KeyValuePair)experimentTypeComboBox.getSelectionModel().getSelectedItem()).getKeyPair());
                target.setExperimentTypeName(((KeyValuePair)experimentTypeComboBox.getSelectionModel().getSelectedItem()).getValuePair());
                target.setInvDate("" + SessionUtils.getAttribute(SessionUtils.WORK_DAY));
                target.setExperimentCode(experimentItemCodeValueLabel.getText());
                target.setExperimentName(experimentItemNameValueLabel.getText());
                target.setRemark(remarkTextField.getText());
                UserEntity userEntity = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
                target.setEntryUserCode(userEntity.getUserCode());
                target.setEntryUserName(userEntity.getUserName());
                target.setFlightsCode(userEntity.getFlightsCode());
                target.setFlightsName(userEntity.getFlightsName());
                target.setEnterpriseId(userEntity.getEnterpriseId());

                try {
                    this.inspectionWeightService.takeToExperiment(target);
                    this.takeToExperimentDialog.hide();
                    (new CustomerAlert(AlertType.INFORMATION, this.resourceBundle.getString("tb_slaughterNumber") + " " + slaughterNumberValueLabel.getText() + " " + this.resourceBundle.getString("info_experiment_success"), new ButtonType[0])).showAndWait();
                    this.query();
                } catch (Exception var11) {
                    var11.printStackTrace();
                    (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("error_experiment"), new ButtonType[0])).showAndWait();
                }

            });
        }

        return this.takeToExperimentDialog;
    }

    private Dialog pigTypeChange(InspectionWeightEntity inspectionWeightEntity) {
        if (this.pigTypeChangeDialog == null) {
            this.pigTypeChangeDialog = new Dialog();
            DialogPane dialogPane = new DialogPane();
            dialogPane.setPrefSize(300.0, 160.0);
            dialogPane.getButtonTypes().addAll(new ButtonType[]{ButtonType.OK, ButtonType.CANCEL});
            Font font = new Font(16.0);
            GridPane gridPane = new GridPane();
            gridPane.setPadding(new Insets(10.0, 10.0, 0.0, 10.0));
            gridPane.setVgap(10.0);
            gridPane.setHgap(10.0);
            Label pigshipnoLabel = new Label(this.resourceBundle.getString("tb_pigshipno"));
            pigshipnoLabel.setFont(font);
            Label pigshipnoValueLabel = new Label(inspectionWeightEntity.getPigshipno());
            pigshipnoValueLabel.setFont(font);
            gridPane.add(pigshipnoLabel, 0, 0);
            GridPane.setHgrow(pigshipnoLabel, Priority.SOMETIMES);
            gridPane.add(pigshipnoValueLabel, 1, 0);
            GridPane.setHgrow(pigshipnoValueLabel, Priority.SOMETIMES);
            Label slaughterNumberLabel = new Label(this.resourceBundle.getString("tb_slaughterNumber"));
            slaughterNumberLabel.setFont(font);
            Label slaughterNumberValueLabel = new Label(inspectionWeightEntity.getSlaughterNo());
            slaughterNumberValueLabel.setFont(font);
            gridPane.add(slaughterNumberLabel, 0, 1);
            gridPane.add(slaughterNumberValueLabel, 1, 1);
            Label pigTypeLabel = new Label(this.resourceBundle.getString("tb_pigType"));
            CustomerComboBox pigTypeComboBox = new CustomerComboBox();
            pigTypeLabel.setFont(font);
            gridPane.add(pigTypeLabel, 0, 2);
            pigTypeLabel.setFont(font);
            gridPane.add(pigTypeComboBox, 1, 2);
            ObservableList<KeyValuePair> pigTypeArrayList = FXCollections.observableArrayList(this.inspectionWeightService.searchPigType());
            pigTypeComboBox.setItems(pigTypeArrayList);

            for(int i = 0; i < pigTypeArrayList.size(); ++i) {
                if (((KeyValuePair)pigTypeArrayList.get(i)).getKeyPair().equals(inspectionWeightEntity.getPigTypeCode())) {
                    pigTypeComboBox.getSelectionModel().select(i);
                    break;
                }
            }

            dialogPane.setContent(gridPane);
            this.pigTypeChangeDialog.setDialogPane(dialogPane);
            this.pigTypeChangeDialog.setTitle(this.resourceBundle.getString("btn_type_change"));
            this.pigTypeChangeDialog.initOwner(ApplicationContext.mainStage);
            Button okBtn = (Button)dialogPane.lookupButton(ButtonType.OK);
            okBtn.setOnMousePressed((event) -> {
                InspectionWeightEntity target = new InspectionWeightEntity();
                target.setEnterpriseId(ApplicationContext.enterpriseId);
                target.setPigshipno(pigshipnoValueLabel.getText());
                target.setPigTypeCode(((KeyValuePair)pigTypeComboBox.getSelectionModel().getSelectedItem()).getKeyPair());
                target.setPigTypeName(((KeyValuePair)pigTypeComboBox.getSelectionModel().getSelectedItem()).getValuePair());

                try {
                    this.inspectionWeightService.changePigType(target);
                    this.pigTypeChangeDialog.hide();
                    this.query();
                } catch (Exception var6) {
                    var6.printStackTrace();
                    (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("error_pigtype_change"), new ButtonType[0])).showAndWait();
                }

            });
        }

        return this.pigTypeChangeDialog;
    }

    public void showEditInspectionWeightDialog() {
        int index = this.inspectionWeightTableView.getSelectionModel().getSelectedIndex();
        if (index < 0) {
            (new CustomerAlert(AlertType.CONFIRMATION, ApplicationContext.commonBundle.getString("choise_one"), new ButtonType[0])).showAndWait();
        } else {
            CommonDialog.loadFXMLDialog(ApplicationContext.commonBundle.getString("common_edit"), "inspectionWeightEdit", "inspectionWeight", new Object[0]).showAndWait();
        }

    }

    public void showExportDialog() {
        this.excelOutHandler.start();
    }

    public void showAddInspectionWeightDialog() {
        InspectionWeightEntity inspectionWeightEntity = this.inspectionWeightService.searchNoWeightPig(this.pigshipno.getText());
        if (inspectionWeightEntity == null) {
            (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("errror_noarrivefactory"), new ButtonType[0])).showAndWait();
        } else {
            Boolean check = this.inspectionWeightService.checkRejection(inspectionWeightEntity);
            if (check == null) {
                (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("errror_noarrivefactory"), new ButtonType[0])).showAndWait();
            } else if (check) {
                this.pigshipno.setText((String)null);
                (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("error_rejection"), new ButtonType[0])).showAndWait();
            }

            Integer count = this.inspectionWeightService.slaughteredCount(inspectionWeightEntity);
            if (count == null) {
                (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("error_noslaughteredCount"), new ButtonType[0])).showAndWait();
            } else if (count < 0) {
                (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("error_slaughterFinished"), new ButtonType[0])).showAndWait();
            }
        }

        CommonDialog.loadFXMLDialog(ApplicationContext.commonBundle.getString("common_add"), "inspectionWeightAdd", "inspectionWeight", new Object[0]).showAndWait();
    }
}
